<?php

class CheckoutController extends Controller
{
    /**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'rights', // perform access control for CRUD operations
		);
	}
    
    public function actionIndex($stadium_id = null,$book_id = null, $table_id = null, $add_stadium = null, $add_table = null, $remove_stadium = null, $remove_table = null)
    {
        $this->layout='//layouts/column1';
        $this->current_page = 'canteen';
        $session=new CHttpSession;
        $session->open();
       
        if ($stadium_id)
        {
            $session['checkout_stadiums'] = array();//array($stadium_id=>$stadium_id);
            $session['checkout_tables'] = array();
        }
        
        if ($table_id)
        {
            $session['checkout_stadiums'] = array();
            $session['checkout_tables'] = array($table_id=>$table_id);
        }
        
        $stadiums = $session['checkout_stadiums'];
        $tables = $session['checkout_tables'];
        
        if ($add_stadium)
        {
            $stadiums = $stadiums + array($add_stadium=>$add_stadium);
            $session['checkout_stadiums'] = $stadiums;
        }
        
        if ($add_table)
        {
            $tables = $tables + array($add_table=>$add_table);
            $session['checkout_tables'] = $tables;
        }
        
        if ($remove_stadium)
        {
            unset($stadiums[$remove_stadium]);
            $session['checkout_stadiums'] = $stadiums;
        }
        
        if ($remove_table)
        {
            unset($tables[$remove_table]);
            $session['checkout_tables'] = $tables;
        }
        
        $total_payment = 0; // tien phai thanh toan
        $total_money = 0;
        
        // Lay thong tin san hien tai
        $bid = 0;
        if($book_id)
        {
            $bookStadium = Jqcalendar::model()->findByPk($book_id);
            if($bookStadium===null)
			throw new CHttpException(404,'The requested page does not exist.');
            $bid = $bookStadium->Bid;
            $stadiums = $session['checkout_stadiums'];
            $stadiums = $stadiums + array($book_id=>$book_id);
            $session['checkout_stadiums'] = $stadiums;
        }
        
        $total_money_stadium = 0;
        $checkoutStadiums = array();
        foreach ($stadiums as $stadium) {
            $calendar = Jqcalendar::model()->checkoutByStadium($stadium);;
            $checkoutStadiums[$calendar['stadium']] = $calendar;
            $total_money_stadium += $checkoutStadiums[$calendar['stadium']]['total'];
            $total_payment = $session["checkout_total_stadium_{$stadium}"] ? $session["checkout_total_stadium_{$stadium}"] : $checkoutStadiums[$calendar['stadium']]['total'];
            $session["checkout_total_stadium_{$stadium}"] = $checkoutStadiums[$calendar['stadium']]['total'];
        }
        $total_money += $total_money_stadium;
        
        $checkoutTables = array();
        foreach ($tables as $table) {
            $checkoutTables[$table] = OrdersTables::model()->checkoutByTable($table);
            $total_money += $checkoutTables[$table]['total_money'];
            $total_payment = $session["checkout_total_table_{$table}"] ? $session["checkout_total_table_{$table}"] : $checkoutTables[$table]['total_money']; 
            $session["checkout_total_table_{$table}"] = $checkoutTables[$table]['total_money'];
        }
        
        if (isset($_POST['Payment']))
        {
            $payment = $_POST['Payment'];

            if ($payment >= $total_payment) {
                $data = array();
                $data['stadiums'] = $checkoutStadiums;
                $data['tables'] = $checkoutTables;
                $data['payment'] = $total_payment;
                $bill = new Bill;
                $bill->user_id = Yii::app()->user->id;
                $bill->data = serialize($data);
                $bill->time = date('Y-m-d H:i:s', time());
                $bill->type = 'checkout';
                $bill->save();
            
                foreach ($tables as $table) {
                    OrdersTables::model()->deleteAllByAttributes(array(
                        'table_id' => $table
                    ));
                }
                
                foreach ($stadiums as $stadium) {
                    $model = Jqcalendar::model()->findByPk($stadium);
                    $model->Description = 'paid';
                    $model->Color = 1;
                    $model->save();
                }
                
                // Tru ben vat tu
                
                foreach ($checkoutTables as $table) {

                  foreach($table['drinks'] as $item)
                  {
                    $number = $item['number'];
                    if($item['drink'])
                    {
                       $drink = $item['drink'];
                       $drink->change($number);
                    }
                    if($item['clothing'])
                    {
                       $clother = $item['clothing'];
                       $clother->change($number);
                    }                    
                  }
                };
                
                $this->redirect($this->createUrl('/canteen'));
            }
        }
        
        $modelCalendar = Jqcalendar::model()->findAll('Description <> :description OR Description IS NULL',array(':description'=>"paid"));
        
        $modelStadium = Stadiums::model()->findAll('visible = 1');
        $totalTables = Yii::app()->settings->get('system', 'table');
        $this->render('index',array(
            'total_money' => $total_money,
            'total_money_stadium' => $total_money_stadium,
            'modelStadium' => $modelStadium,
            'totalTables' => $totalTables,
            'modelCalendar' => $modelCalendar,
            'checkoutStadiums' => $checkoutStadiums,
            'checkoutTables' => $checkoutTables,
        ));
    }
    
    public function actionSaveTotalMoney($type, $id, $money, $promotion)
    {
        $session=new CHttpSession;
        $session->open();
        $session["checkout_total_{$type}_{$id}"] = $money;
        $session["checkout_total_{$type}_{$id}_promotion"] = $promotion;
    }
    
    public function actionPrint()
    {
        $session=new CHttpSession;
        $session->open();
        
        $stadiums = $session['checkout_stadiums'];
        $tables = $session['checkout_tables'];
        
        $total_money = 0;
        
        $total_money_stadium = 0;
        $checkoutStadiums = array();
        foreach ($stadiums as $stadium) {
            $calendar = Jqcalendar::model()->checkoutByStadium($stadium);;
            $checkoutStadiums[$calendar['stadium']] = $calendar;
            if ($session["checkout_total_stadium_{$stadium}"]) $checkoutStadiums[$calendar['stadium']]['total'] = $session["checkout_total_stadium_{$stadium}"];
            $checkoutStadiums[$calendar['stadium']]['promotion'] = $session["checkout_total_stadium_{$stadium}_promotion"] ? $session["checkout_total_stadium_{$stadium}_promotion"] : 0;
            $total_money_stadium += $checkoutStadiums[$calendar['stadium']]['total'];
        }
        $total_money += $total_money_stadium;
        
        $checkoutTables = array();
        foreach ($tables as $table) {
            $checkoutTables[$table] = OrdersTables::model()->checkoutByTable($table);
            if ($session["checkout_total_table_{$table}"]) $checkoutTables[$table]['total_money'] = $session["checkout_total_table_{$table}"];
            $checkoutTables[$table]['promotion'] = $session["checkout_total_table_{$table}_promotion"] ? $session["checkout_total_table_{$table}_promotion"] : 0;
            $total_money += $checkoutTables[$table]['total_money'];
        }
        
        $modelCalendar = Jqcalendar::model()->findAll('Description <> :description OR Description IS NULL',array(':description'=>"paid"));
        
        $modelStadium = Stadiums::model()->findAll('visible = 1');
        $totalTables = Yii::app()->settings->get('system', 'table');
        $this->renderPartial('print',array(
            'total_money' => $total_money,
            'total_money_stadium' => $total_money_stadium,
            'modelStadium' => $modelStadium,
            'totalTables' => $totalTables,
            'modelCalendar' => $modelCalendar,
            'checkoutStadiums' => $checkoutStadiums,
            'checkoutTables' => $checkoutTables,
        ));
    }
}